//
//  ClassifyViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ClassifyViewController.h"
#import "UIImageView+WebCache.h"
#import "ZWYNetTool.h"
#import "ClassifyTableViewCell.h"
#import "TextView.h"
#import "TAMacro.h"

#import "ClassFirst.h"
#import "ClassifyData.h"
#import "ClassItems.h"
#import "ClassifySecond.h"

#import "CellDetailViewController.h"
#import "TextLabelTableViewCell.h"

#import "MBProgressHUD.h"
#import <QuartzCore/QuartzCore.h>

@interface ClassifyViewController () <UITableViewDelegate, UITableViewDataSource, sendCatID>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *classifyTableView;
@property (nonatomic, strong) NSMutableArray *picDataArray;
@property (nonatomic, strong) NSMutableArray *cellDataArray;

@property (nonatomic, strong) NSMutableArray *classifyArray;

@property (nonatomic, strong) ClassFirst *classFirst;

@property (nonatomic, copy) NSString *reuse;
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ClassifyViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self createData];
//    [self createHud];
    
}
- (void)createHud {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
    
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        self.page += self.page;
        [self createData];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hideAnimated:YES afterDelay:1];
        });
    });
}



- (void)createData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");

    self.page = 1;
    self.picDataArray = [NSMutableArray array];
    self.cellDataArray = [NSMutableArray array];
    self.classifyArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    
    NSString *str = @"http://mobile.iliangcang.com/goods/goodsCategory?a=b&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.classFirst = [[ClassFirst alloc] init];
        self.classFirst = [ClassFirst modelWithDic:result];
        // 注意这个model second 是数组套数组套model
//        NSLog(@"%@", self.classFirst.data.items[0].second[0][0].cat_name);
        for (ClassItems *items in self.classFirst.data.items) {
            [self.dataArray addObject:items];
        }
        if (self.dataArray.count != 0) {
            [hud hideAnimated:YES afterDelay:0.5];
        }
        
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        }];
}

- (void)createTableView {
    self.view.backgroundColor = [UIColor colorWithWhite:0.094 alpha:1.000];
;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 99, WIDTH, HEIGHT - 99 - 44) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.094 alpha:1.000];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 230;
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:self.tableView]) {
    return self.classFirst.data.items.count;
    } else {
        return 7;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = @"reuse";
    self.reuse = [str stringByAppendingString:[NSString stringWithFormat:@"%ld", indexPath.row]];
        ClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.reuse];
    if (!cell) {
        cell = [[ClassifyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.reuse];
    }
        cell.classifyTableView.hidden = NO;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.classFirst.data.items[indexPath.row].cover_img]]];
        cell.backgroundColor = [UIColor grayColor];
        return cell;
  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.tableView]) {
       ClassifyTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        // 传一个数组 给cell中的tableView
        cell.tableArray = self.classFirst.data.items[indexPath.row].second[0];
        cell.itemID = self.classFirst.data.items[indexPath.row].cat_id;
        cell.delegate = self;
        // 弹出tableVIew的动画
        if (cell.table_width == -350) {
            cell.table_width = 0;
            [UIView animateWithDuration:1 animations:^{
                cell.classifyTableView.frame = CGRectMake(cell.table_width, 0, cell.frame.size.width - 70, cell.frame.size.height);
            }];
            } else {
                cell.table_width = -350;
                [UIView animateWithDuration:1 animations:^{
                    cell.classifyTableView.frame = CGRectMake(cell.table_width, 0, cell.frame.size.width - 70, cell.frame.size.height);
            }];
        }
    }
}
// 获取上次点击的cell
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.tableView]) {
        ClassifyTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.table_width = -350;
            [UIView animateWithDuration:1 animations:^{
                cell.classifyTableView.frame = CGRectMake(cell.table_width, 0, cell.frame.size.width - 70, cell.frame.size.height);
            }];
    }
}

- (void)sendID:(NSString *)idStr secID:(NSString *)secID{
    CellDetailViewController *detail = [[CellDetailViewController alloc] init];
    detail.urlStr = idStr;
    detail.secID = secID;
    [self.navigationController pushViewController:detail animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
